package com.yorick.authority.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yorick.authority.dao.*;
import com.yorick.authority.entitys.*;
import com.yorick.authority.util.TreeRecursion;
import com.yorick.common.config.jedis.JedisClient;
import com.yorick.common.dao.SecurityMapper;
import com.yorick.login.dao.LoginMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class AbstractAuthorityService extends TreeRecursion {
    @Resource
    protected LoginMapper loginDao;
    @Resource
    protected AccountsMapper accountsMapper;
    @Resource
    protected RoleMapper roleMapper;
    @Resource
    protected DepartmentMapper departmentMapper;
    @Resource
    protected MenuMapper menuMapper;
    @Resource
    protected IPWhiteListMapper ipWhiteListMapper;
    @Resource
    protected SecurityMapper securityMapper;
    @Resource
    protected JedisClient jedisClient;
    @Resource
    protected ObjectMapper objectMapper;


    @Override
    protected JSONObject customizeNode(Node node) {
        return null;
    }

    //根据账户ID获取所属机构所有子机构ID
    protected List<Integer> getDID(int aid){
        List<Integer> result = new ArrayList<>(Arrays.asList(0));
        List<Integer> departments = departmentMapper.findDepartmentByAID(aid);
        departments.parallelStream().forEach(
                department ->{
                    result.addAll(departmentMapper.findChildrenByDID(department));
                }
        );
        return result;
    }
    //根据账户ID获取所属机构所有子机构名称
    protected List<String> getDName(int aid){
        List<DepartmentDto> departments = departmentMapper.findDepartmentByDID(getDID(aid));
        List<String> result = new ArrayList<>();
        departments.stream().forEach(
                department ->{
                    result.add(department.getDname());
                }
        );
        return result;
    }


    protected void insertUserRole(List<Integer> roleList, int aid) {
        roleList.stream().forEach(
                roleId -> {
                    UserRoleDto userRoleDto = UserRoleDto.builder()
                            .roleId(roleId)
                            .aid(aid)
                            .build();
                    accountsMapper.insertUserRole(userRoleDto);
                }
        );
    }

    protected void insertUserDepartment(List<Integer> departmentList, int aid) {
        departmentList.stream().forEach(
                did -> {
                    UserDepartmentDto userDepartmentDto = UserDepartmentDto.builder()
                            .did(did)
                            .aid(aid)
                            .build();
                    accountsMapper.insertUserDepartment(userDepartmentDto);
                }
        );
    }

    protected void insertRoleResource(List<Integer> resourceId, int roleId) {
        resourceId.stream().forEach(
                resource -> {
                    RoleResourceDto roleResourceDto = RoleResourceDto.builder()
                            .roleId(roleId)
                            .resourceId(resource)
                            .build();
                    roleMapper.insertRoleResource(roleResourceDto);
                }
        );
    }

    protected void insertRoleDepartment(List<Integer> departmentList, int roleId){
        departmentList.stream().forEach(
                did -> {
                    RoleDepartmentDto roleDepartmentDto = RoleDepartmentDto.builder()
                            .roleId(roleId)
                            .did(did)
                            .build();
                    roleMapper.insertRoleDepartment(roleDepartmentDto);
                }
        );
    }
}
